package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constants.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.AuthMapper;
import com.usian.user.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements AuthService {
    @Resource
   private ApUserMapper apUserMapper;
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        //1.判断authDto是否为null
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.检查分页参数
        authDto.checkParam();
        //3.创建条件构造器
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(authDto.getStatus()!=null, ApUserRealname::getStatus,authDto.getStatus() );
        //4.创建Page对象
        Page page=new Page(authDto.getPage(),authDto.getSize());
        //5.实现分页
        Page page1 = page(page, queryWrapper);
        //6.封装
        ResponseResult result=new PageResponseResult(authDto.getPage(),authDto.getSize(), (int) page1.getTotal());
        result.setData(page1.getRecords());
        return result;
    }

    @Override
    public ResponseResult updateStatusById(AuthDto dto, Integer status) {
        //检查参数
        if (dto==null||dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查状态
        if (checkStatus(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改realName状态
        ApUserRealname userRealname = new ApUserRealname();
        userRealname.setId(dto.getId());
        userRealname.setStatus(status);
        if(dto.getMsg()!=null){
            userRealname.setReason(dto.getMsg());
        }
        if (status.equals(UserConstants.PASS_AUTH)){
            ResponseResult result = createWemediaAndAuthor(dto);
            if (result!=null){
                return result;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    public ResponseResult createWemediaAndAuthor(AuthDto dto){
        ApUserRealname realname = getById(dto.getId());
        ApUser apUser = apUserMapper.selectById(realname.getUserId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser==null){
             wmUser = new WmUser();
             wmUser.setApUserId(apUser.getId());
             wmUser.setCreatedTime(new Date());
             wmUser.setSalt(apUser.getSalt());
             wmUser.setName(apUser.getName());
             wmUser.setPassword(apUser.getPassword());
             wmUser.setStatus(9);
             wmUser.setPhone(apUser.getPhone());
             wemediaFeign.save(wmUser);
        }
        createAuthor(wmUser);
        return null;
    }

    public void createAuthor(WmUser wmUser){
        ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
        if (apAuthor==null){
            apAuthor=new ApAuthor();
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(UserConstants.AUTHOR_TYPE);
            apAuthor.setCreatedTime(new Date());
            apAuthor.setUserId(wmUser.getApUserId());
            articleFeign.save(apAuthor);
        }
    }

    public boolean checkStatus(Integer status){
        if (status!=null){
            return false;
        }
        return true;
    }
}
